
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 02/11/2011 19:49:43
-- Generated from EDMX file: C:\Users\施懿民\documents\visual studio 2010\Projects\mynote\mynote.data\Models\NoteModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [note];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_NoteTag_Note]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NoteTag] DROP CONSTRAINT [FK_NoteTag_Note];
GO
IF OBJECT_ID(N'[dbo].[FK_NoteTag_Tag]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NoteTag] DROP CONSTRAINT [FK_NoteTag_Tag];
GO
IF OBJECT_ID(N'[dbo].[FK_ReferenceNote_inherits_Note]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NoteSet_ReferenceNote] DROP CONSTRAINT [FK_ReferenceNote_inherits_Note];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[NoteSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NoteSet];
GO
IF OBJECT_ID(N'[dbo].[TagSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TagSet];
GO
IF OBJECT_ID(N'[dbo].[NoteSet_ReferenceNote]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NoteSet_ReferenceNote];
GO
IF OBJECT_ID(N'[dbo].[NoteTag]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NoteTag];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'NoteSet'
CREATE TABLE [dbo].[NoteSet] (
    [Id] uniqueidentifier  NOT NULL,
    [Owner] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Tag] nvarchar(max)  NULL,
    [CreatedDate] datetime  NOT NULL,
    [LastModified] datetime  NOT NULL,
    [IsRead] bit  NOT NULL,
    [Title] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'TagSet'
CREATE TABLE [dbo].[TagSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'NoteSet_ReferenceNote'
CREATE TABLE [dbo].[NoteSet_ReferenceNote] (
    [Url] nvarchar(max)  NOT NULL,
    [IsMaterialized] bit  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'NoteTag'
CREATE TABLE [dbo].[NoteTag] (
    [Notes_Id] uniqueidentifier  NOT NULL,
    [Tags_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'NoteSet'
ALTER TABLE [dbo].[NoteSet]
ADD CONSTRAINT [PK_NoteSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'TagSet'
ALTER TABLE [dbo].[TagSet]
ADD CONSTRAINT [PK_TagSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'NoteSet_ReferenceNote'
ALTER TABLE [dbo].[NoteSet_ReferenceNote]
ADD CONSTRAINT [PK_NoteSet_ReferenceNote]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Notes_Id], [Tags_Id] in table 'NoteTag'
ALTER TABLE [dbo].[NoteTag]
ADD CONSTRAINT [PK_NoteTag]
    PRIMARY KEY NONCLUSTERED ([Notes_Id], [Tags_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Notes_Id] in table 'NoteTag'
ALTER TABLE [dbo].[NoteTag]
ADD CONSTRAINT [FK_NoteTag_Note]
    FOREIGN KEY ([Notes_Id])
    REFERENCES [dbo].[NoteSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Tags_Id] in table 'NoteTag'
ALTER TABLE [dbo].[NoteTag]
ADD CONSTRAINT [FK_NoteTag_Tag]
    FOREIGN KEY ([Tags_Id])
    REFERENCES [dbo].[TagSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_NoteTag_Tag'
CREATE INDEX [IX_FK_NoteTag_Tag]
ON [dbo].[NoteTag]
    ([Tags_Id]);
GO

-- Creating foreign key on [Id] in table 'NoteSet_ReferenceNote'
ALTER TABLE [dbo].[NoteSet_ReferenceNote]
ADD CONSTRAINT [FK_ReferenceNote_inherits_Note]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[NoteSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------